﻿using StructureMap;
using WholesaleOnline.Contract;
using WholesaleOnline.Controller.Storage;

namespace WholesaleOnline.Controller.Handlers
{
    public class HandlerFactory : IHandlerFactory
    {
        public RequestHandler GetHandlers()
        {
            RequestHandler handlers = new CheckLoginHandler(ObjectFactory.GetInstance<IHttpStorage>());

            handlers
                .SetNextHandler(CreateGetProductsAndCategoriesHandler())
                .SetNextHandler(CreateGetProductsHandler())
                .SetNextHandler(CreateGetKeywordsHandler())
                .SetNextHandler(CreateAddToBasketHanlder())
                .SetNextHandler(CreateGetBasketsHandler())
                .SetNextHandler(CreateUpdateBasketProductQtyHandler())
                .SetNextHandler(CreateRemoveBasketItemHandler())
                .SetNextHandler(CreateInvalidHandler());

            return handlers;
        }

        private GetProductsAndCategoriesHandler CreateGetProductsAndCategoriesHandler()
        {
            return new GetProductsAndCategoriesHandler(
                ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IInventoryService>());
        }

        private GetProductsHandler CreateGetProductsHandler()
        {
            return new GetProductsHandler(ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IInventoryService>());
        }

        private GetKeywordsHandler CreateGetKeywordsHandler()
        {
            return new GetKeywordsHandler(ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IInventoryService>());
        }

        private AddToBasketHanlder CreateAddToBasketHanlder()
        {
            return new AddToBasketHanlder(ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IBasketService>());
        }

        public GetBasketsHandler CreateGetBasketsHandler()
        {
            return new GetBasketsHandler(ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IBasketService>());
        }

        private UpdateBasketProductQtyHandler CreateUpdateBasketProductQtyHandler()
        {
            return new UpdateBasketProductQtyHandler(
                ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IBasketService>());
        }

        private RemoveBasketItemHandler CreateRemoveBasketItemHandler()
        {
            return new RemoveBasketItemHandler(ObjectFactory.GetInstance<IHttpStorage>(), ObjectFactory.GetInstance<IBasketService>());
        }

        private InvalidHandler CreateInvalidHandler()
        {
            return new InvalidHandler(ObjectFactory.GetInstance<IHttpStorage>());
        }
    }
}
